﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Platform.Model;
using BLToolkit.Data.Linq;
using System.Linq;
using Platform.Model;

namespace PlatformManagement
{
    public partial class PickingForm : DevComponents.DotNetBar.Metro.MetroForm
    {
        public PickingForm()
        {
            InitializeComponent();

        }
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            this.WindowState = FormWindowState.Maximized;
        }
        protected override void OnLoad(EventArgs e)
        {
            //var bindingSource = new BindingSource { DataSource = PickingList.Objects.All };
            //dataGridViewX1.DataSource = bindingSource;
            //var bindingSource2 = new BindingSource { DataSource = PickingDetail.Objects.All };
            //dataGridViewX2.DataSource = bindingSource2;
            base.OnLoad(e);
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            slideLeft.IsOpen = false;
        }

        private void PickingForm_Load(object sender, EventArgs e)
        {
            List<Supplier> list = Supplier.Objects.GetAll();
            dgSupplier.AutoGenerateColumns = false;
            dgSupplier.ColumnCount = 3;
            dgSupplier.DataSource = list;


            List<Article> lista = Article.Objects.GetAll();
            dgArticle.AutoGenerateColumns = false;
            dgArticle.ColumnCount = 2;
            dgArticle.DataSource = lista;

            List<PickingList> listl = PickingList.Objects.GetAll();
            dataGridViewX1.AutoGenerateColumns = false;
            dataGridViewX1.ColumnCount = 14;
            dataGridViewX1.DataSource = listl;


            List<PickingDetail> listd = PickingDetail.Objects.GetAll();
            dataGridViewX2.AutoGenerateColumns = false;
            dataGridViewX2.ColumnCount = 17;
            dataGridViewX2.DataSource = listd;
            base.OnShown(e);
            LoadCCB();
        }


        private void dgSupplier_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }



        //lay san pham theo id
        Article SanPham(int id)
        {
            Article a = null;
            List<Article> list = Article.Objects.GetAll();
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].ID == id)
                    {
                        a = list[i];
                    }
                }
            }
            return a;

        }
        //lay sieu thi theo id
        Store SieuThi(int id)
        {
            Store a = null;
            List<Store> list = Store.Objects.GetAll();
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Id == id)
                    {
                        a = list[i];
                    }
                }
            }
            return a;

        }
        //lay nha cung cap theo id
        Supplier NhaCC(int id)
        {
            Supplier s = null;
            List<Supplier> list = Supplier.Objects.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ID == id)
                {
                    s = list[i];
                }
            }
            return s;
        }

        //lay pickinglist theo id
        PickingList LayPickingList(int id)
        {
            PickingList a = null;
            List<PickingList> list = PickingList.Objects.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ID == id)
                {
                    a = list[i];
                }
            }
            return a;

        }

        public static string mancc1 = "";
        //chọn nhà cung cấp
        private void dgSupplier_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgSupplier.CurrentRow.Cells[0].Value.ToString() != "")
            {
                string ma = dgSupplier.CurrentRow.Cells[0].Value.ToString();

                Supplier sup = Supplier.Objects.FirstOrDefault(x => x.SupNo == ma);
                int mancc = Convert.ToInt32(sup.SupNo.ToString());
                List<PickingDetail> list = PickingDetail.Objects.Where(x => x.SupId == sup.ID).ToList();
                List<int> listPickingId = new List<int>();
                // lấy danh sách của pickingIDd trong PickingDetail
                foreach(PickingDetail pc in list)
                {
                    if (!KTTrung(pc.PickingId, listPickingId))
                        listPickingId.Add(pc.PickingId);
                }
                List<PickingList> listpickingList = new List<PickingList>();
                foreach(int id in listPickingId)
                {
                    PickingList pl = PickingList.Objects.FirstOrDefault(x => x.ID == id);
                    listpickingList.Add(pl);
                }
                dataGridViewX1.DataSource = listpickingList;
                for (int i = 0; i < listpickingList.Count; i++)
                {

                    //dataGridViewX2.DataSource = list2;
                    dataGridViewX1.Rows[i].Cells[0].Value = listpickingList[i].ID;
                    dataGridViewX1.Rows[i].Cells[1].Value = listpickingList[i].ArtId;
                    string id = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
                    Article a = SanPham(Convert.ToInt32(id));
                    dataGridViewX1.Rows[i].Cells[2].Value = a.ArtNo.ToString();
                    dataGridViewX1.Rows[i].Cells[3].Value = listpickingList[i].DoId;
                    dataGridViewX1.Rows[i].Cells[4].Value = listpickingList[i].StoreId;
                    dataGridViewX1.Rows[i].Cells[5].Value = listpickingList[i].OrderNo;
                    dataGridViewX1.Rows[i].Cells[6].Value = listpickingList[i].OrderQty;
                    dataGridViewX1.Rows[i].Cells[7].Value = listpickingList[i].Gross;
                    dataGridViewX1.Rows[i].Cells[8].Value = listpickingList[i].Net;
                    dataGridViewX1.Rows[i].Cells[9].Value = listpickingList[i].Package;
                    dataGridViewX1.Rows[i].Cells[10].Value = listpickingList[i].NLoss;
                    dataGridViewX1.Rows[i].Cells[11].Value = listpickingList[i].PLoss;
                    dataGridViewX1.Rows[i].Cells[12].Value = listpickingList[i].StoreName;
                    dataGridViewX1.Rows[i].Cells[13].Value = listpickingList[i].LastUpdated;
                }

                //sản phẩm
                List<Article> lista = Article.Objects.GetAll();
                List<Article> lista2 = new List<Article>();
                foreach (Article c in lista)
                {
                    foreach (PickingList a in listpickingList)
                    {
                        if (c.ID == a.ArtId)
                        {
                            lista2.Add(c);
                        }
                    }
                }
                //load DL
                dgArticle.DataSource = lista2;
                //load pickinglist
                //dataGridViewX1.DataSource = listl2;
                //////////////////////////
                List<PickingDetail> lisspicd = new List<PickingDetail>();
                dataGridViewX2.DataSource = lisspicd;
            }

        }
        public bool KTTrung(int x, List<int> list)
        {
            foreach(int t in list)
            {
                if(t==x)
                    return true;
            }
            return false;
        }
        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //sự kiện click vào pickinglist
        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewX2.DataSource = null;
            if (dataGridViewX1.CurrentRow.Cells[0].Value.ToString() != "")
            {
                int pickid = Convert.ToInt32(dataGridViewX1.CurrentRow.Cells[0].Value.ToString());
                float socandat = float.Parse(dataGridViewX1.CurrentRow.Cells[6].Value.ToString());
                List<PickingDetail> list = PickingDetail.Objects.Where(x => x.PickingId == pickid).ToList();
                             
                PickingDetail a = new PickingDetail();

                //foreach (PickingDetail pd in list)
                //{
                //    a = pd;
                //}


                //PickingList p = new PickingList();
                //p = PickingList.Objects.First(x => x.ID == int.Parse(pickid));
                //if (p.Net > p.OrderQty)
                //{
                //    dataGridViewX2.Enabled = false;
                //}
                //else
                //{
                //    dataGridViewX2.Enabled = true;
                //}

                float soluongdacan = SoLuongDaCan(list);
                if (list.Count > 0)
                {
                    if (soluongdacan >= socandat)
                    {
                        for (int i = 0; i < list.Count - 1; i++)
                        {
                            dataGridViewX2.DataSource = list;
                            dataGridViewX2.Rows[i].Cells[0].Value = list[i].PickingTime;
                            dataGridViewX2.Rows[i].Cells[1].Value = list[i].PickingId;
                            dataGridViewX2.Rows[i].Cells[2].Value = list[i].SupId;
                            string id = dataGridViewX2.Rows[i].Cells[2].Value.ToString();
                            Supplier s = NhaCC(Convert.ToInt32(id));
                            dataGridViewX2.Rows[i].Cells[3].Value = s.FirstName + "" + s.LastName;
                            string picid = dataGridViewX2.Rows[i].Cells[1].Value.ToString();
                            PickingList pickl = LayPickingList(Convert.ToInt32(picid));
                            Article art = SanPham(pickl.ArtId);
                            dataGridViewX2.Rows[i].Cells[4].Value = art.ArtName;
                            dataGridViewX2.Rows[i].Cells[5].Value = list[i].Gross;
                            dataGridViewX2.Rows[i].Cells[6].Value = list[i].Net;
                            dataGridViewX2.Rows[i].Cells[7].Value = list[i].Large;
                            dataGridViewX2.Rows[i].Cells[8].Value = list[i].Medium;
                            dataGridViewX2.Rows[i].Cells[9].Value = list[i].Small;
                            dataGridViewX2.Rows[i].Cells[10].Value = list[i].Tiny;
                            dataGridViewX2.Rows[i].Cells[11].Value = list[i].NLoss;
                            dataGridViewX2.Rows[i].Cells[12].Value = list[i].PLoss;
                            dataGridViewX2.Rows[i].Cells[13].Value = list[i].LastUpdated;
                            dataGridViewX2.Rows[i].Cells[14].Value = list[i].WeightedBy;
                            dataGridViewX2.Rows[i].Cells[15].Value = list[i].Notes;
                            dataGridViewX2.Rows[i].Cells[16].Value = list[i].ID;

                        }

                        btnPick.Visible = false;
                        btnPickNo.Visible = false;
                        dataGridViewX2.Enabled = false;
                    }
                    if (soluongdacan < socandat && soluongdacan > 0)
                    {
                        dataGridViewX2.Enabled = true;
                        a.PickingTime = list[list.Count - 1].PickingTime;
                        list.Add(a);
                        dataGridViewX2.DataSource = list;
                        for (int i = 0; i < list.Count - 1; i++)
                        {
                            dataGridViewX2.DataSource = list;
                            dataGridViewX2.Rows[i].Cells[0].Value = list[i].PickingTime;
                            dataGridViewX2.Rows[i].Cells[1].Value = list[i].PickingId;
                            dataGridViewX2.Rows[i].Cells[2].Value = list[i].SupId;
                            string id = dataGridViewX2.Rows[i].Cells[2].Value.ToString();
                            Supplier sp = NhaCC(Convert.ToInt32(id));
                            dataGridViewX2.Rows[i].Cells[3].Value = sp.FirstName + "" + sp.LastName;
                            string picid = dataGridViewX2.Rows[i].Cells[1].Value.ToString();
                            PickingList pickl = LayPickingList(Convert.ToInt32(picid));
                            Article art = SanPham(pickl.ArtId);
                            dataGridViewX2.Rows[i].Cells[4].Value = art.ArtName;
                            dataGridViewX2.Rows[i].Cells[5].Value = list[i].Gross;
                            dataGridViewX2.Rows[i].Cells[6].Value = list[i].Net;
                            dataGridViewX2.Rows[i].Cells[7].Value = list[i].Large;
                            dataGridViewX2.Rows[i].Cells[8].Value = list[i].Medium;
                            dataGridViewX2.Rows[i].Cells[9].Value = list[i].Small;
                            dataGridViewX2.Rows[i].Cells[10].Value = list[i].Tiny;
                            dataGridViewX2.Rows[i].Cells[11].Value = list[i].NLoss;
                            dataGridViewX2.Rows[i].Cells[12].Value = list[i].PLoss;
                            dataGridViewX2.Rows[i].Cells[13].Value = list[i].LastUpdated;
                            dataGridViewX2.Rows[i].Cells[16].Value = list[i].ID;
                        }
                        btnPick.Visible = false;
                        btnPickNo.Visible = true;
                    }
                }
                if (soluongdacan <= 0)
                {
                    dataGridViewX2.Enabled = true;

                    for (int i = 0; i < list.Count - 1; i++)
                    {

                        dataGridViewX2.DataSource = list;
                        dataGridViewX2.Rows[i].Cells[0].Value = 1;
                        dataGridViewX2.Rows[i].Cells[1].Value = list[i].PickingId;
                        dataGridViewX2.Rows[i].Cells[2].Value = list[i].SupId;
                        string id = dataGridViewX2.Rows[i].Cells[2].Value.ToString();
                        Supplier s = NhaCC(Convert.ToInt32(id));
                        dataGridViewX2.Rows[i].Cells[3].Value = s.FirstName + "" + s.LastName;
                        string picid = dataGridViewX2.Rows[i].Cells[1].Value.ToString();
                        PickingList pickl = LayPickingList(Convert.ToInt32(picid));
                        Article art = SanPham(pickl.ArtId);
                        dataGridViewX2.Rows[i].Cells[4].Value = art.ArtName;
                        dataGridViewX2.Rows[i].Cells[5].Value = list[i].Gross;
                        dataGridViewX2.Rows[i].Cells[6].Value = list[i].Net;
                        dataGridViewX2.Rows[i].Cells[7].Value = list[i].Large;
                        dataGridViewX2.Rows[i].Cells[8].Value = list[i].Medium;
                        dataGridViewX2.Rows[i].Cells[9].Value = list[i].Small;
                        dataGridViewX2.Rows[i].Cells[10].Value = list[i].Tiny;
                        dataGridViewX2.Rows[i].Cells[11].Value = list[i].NLoss;
                        dataGridViewX2.Rows[i].Cells[12].Value = list[i].PLoss;
                        dataGridViewX2.Rows[i].Cells[13].Value = list[i].LastUpdated;
                        dataGridViewX2.Rows[i].Cells[14].Value = list[i].WeightedBy;
                        dataGridViewX2.Rows[i].Cells[15].Value = list[i].Notes;
                        dataGridViewX2.Rows[i].Cells[16].Value = list[i].ID;
                    }
                    btnPick.Visible = true;
                    btnPickNo.Visible = false;
                }
            }



        }
        List<PickingDetail> ListPickDetail(int ma)
        {
            List<PickingDetail> list = new List<PickingDetail>();
            List<PickingDetail> list1 = PickingDetail.Objects.GetAll();

            foreach (var a in list1)
            {
                if (a.PickingId == ma)
                {
                    list.Add(a);
                }
            }
            return list;
        }
        float SoLuongDaCan(List<PickingDetail> list)
        {
            float tong = 0;
            foreach (PickingDetail a in list)
            {
                tong = tong + a.Net;
            }
            return tong;
        }

        void LoadPickList(string supId)
        {

            // doan nay ne, nhung ma dai qua, hehe
            string ma = dgSupplier.CurrentRow.Cells[0].Value.ToString();
            List<Supplier> listncc = Supplier.Objects.GetAll();
            List<Supplier> listncc2 = new List<Supplier>();
            foreach (Supplier a in listncc)
            {
                if (a.SupNo == ma)
                {
                    listncc2.Add(a);
                }
            }
            string mancc = listncc2[0].ID.ToString();
            ///////
            mancc1 = mancc;
            //lấy hóa đơn theo mã ncc 
            List<PickingDetail> list1 = PickingDetail.Objects.GetAll();
            List<PickingDetail> list2 = new List<PickingDetail>();
            foreach (PickingDetail a in list1)
            {
                DateTime ngay = a.LastUpdated;
                if (a.SupId == Convert.ToInt32(mancc) && DateTime.Now.Day == ngay.Day && DateTime.Now.Month == ngay.Month && DateTime.Now.Year == ngay.Year)
                {
                    list2.Add(a);
                }
            }

            List<PickingList> listl1 = PickingList.Objects.GetAll();
            List<PickingList> listl2 = new List<PickingList>();
            foreach (PickingList b in listl1)
            {
                foreach (PickingDetail a in list2)
                {
                    if (a.PickingId == b.ID)
                    {
                        listl2.Add(b);
                    }
                }
            }

            //sản phẩm
            List<Article> lista = Article.Objects.GetAll();
            List<Article> lista2 = new List<Article>();
            foreach (Article c in lista)
            {
                foreach (PickingList a in listl2)
                {
                    if (c.ID == a.ArtId)
                    {
                        lista2.Add(c);
                    }
                }
            }
            //load DL
            dgArticle.DataSource = lista2;
            //load pickinglist
            //dataGridViewX1.DataSource = listl2;
            //////////////////////////
            List<PickingDetail> lisspicd = new List<PickingDetail>();
            dataGridViewX2.DataSource = lisspicd;

            for (int i = 0; i < listl2.Count; i++)
            {

                //dataGridViewX2.DataSource = list2;
                dataGridViewX1.Rows[i].Cells[0].Value = listl2[i].ID;
                dataGridViewX1.Rows[i].Cells[1].Value = listl2[i].ArtId;
                string id = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
                Article a = SanPham(Convert.ToInt32(id));
                dataGridViewX1.Rows[i].Cells[2].Value = a.ArtNo.ToString();
                dataGridViewX1.Rows[i].Cells[3].Value = listl2[i].DoId;
                dataGridViewX1.Rows[i].Cells[4].Value = listl2[i].StoreId;
                dataGridViewX1.Rows[i].Cells[5].Value = listl2[i].OrderNo;
                dataGridViewX1.Rows[i].Cells[6].Value = listl2[i].OrderQty;
                dataGridViewX1.Rows[i].Cells[7].Value = listl2[i].Gross;
                dataGridViewX1.Rows[i].Cells[8].Value = listl2[i].Net;
                dataGridViewX1.Rows[i].Cells[9].Value = listl2[i].Package;
                dataGridViewX1.Rows[i].Cells[10].Value = listl2[i].NLoss;
                dataGridViewX1.Rows[i].Cells[11].Value = listl2[i].PLoss;
                dataGridViewX1.Rows[i].Cells[12].Value = listl2[i].StoreName;
                dataGridViewX1.Rows[i].Cells[13].Value = listl2[i].LastUpdated;

            }
        }

        //xu ly PICK
        private void btnPick_Click(object sender, EventArgs e)
        {
            string supId = "";
            int cout = dataGridViewX2.Rows.Count;
            bool kqupdate = false;
            PickingList p = new PickingList();
            int pickingid = Convert.ToInt32(dataGridViewX2.Rows[0].Cells[1].Value.ToString());
            if (cout > 0)
            {
                p = PickingList.Objects.First(x => x.ID == pickingid);
            }
            p.Gross =0;
            p.Net =0;
            p.NLoss =0;
            p.PLoss=0;
            p.Package =0;
            for (int i = 0; i < cout-1; i++)
            {
                PickingDetail a = new PickingDetail();

                a.ID = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[16].Value.ToString());
                a.PickingTime = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[0].Value.ToString());
                a.PickingId = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[1].Value.ToString());
                a.SupId = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[2].Value.ToString());
                a.Gross = float.Parse(dataGridViewX2.Rows[i].Cells[5].Value.ToString());
                a.Net = float.Parse(dataGridViewX2.Rows[i].Cells[6].Value.ToString());
                a.Large = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[7].Value.ToString());
                a.Medium = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[8].Value.ToString());
                a.Small = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[9].Value.ToString());
                a.Tiny = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[10].Value.ToString());
                a.NLoss = float.Parse(dataGridViewX2.Rows[i].Cells[11].Value.ToString());
                a.PLoss = float.Parse(dataGridViewX2.Rows[i].Cells[12].Value.ToString());
                string s = dataGridViewX2.Rows[i].Cells[14].Value.ToString();
                if (s == "")
                {
                    a.WeightedBy = 0;
                }
                else
                {
                    a.WeightedBy = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[14].Value.ToString());
                }
                a.LastUpdated = DateTime.Now;
                a.Notes = dataGridViewX2.Rows[i].Cells[15].Value.ToString();


                
                p.Gross  += a.Gross;
                p.Net += a.Net;
                p.NLoss += a.NLoss;
                p.PLoss  += a.PLoss;
                p.Package +=(  a.Large + a.Small + a.Medium + a.Tiny);

                PickingDetail pd = PickingDetail.Objects.FirstOrDefault(x => x.ID == a.ID);
                if (pd != null)
                {
                    // cap nhat pickDetail
                    bool rs = PickingDetail.Objects.Update(a);
                }
                else
                {
                    PickingDetail.Objects.Insert(a);
                }
                // Load, doa nay dung roi
                supId = a.SupId + "";


            }
            // cap nhat pickList
            kqupdate = PickingList.Objects.Update(p);
            if(kqupdate)
            MessageBox.Show("Hoàn thành cân hàng");
            else
                MessageBox.Show("Chưa hoàn thành cân hàng");
            LoadPickList(supId);




        }

        private void dataGridViewX2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        //nhập giá trị cho pickingdetail
        private void dataGridViewX2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int l = 0, med = 0, sm = 0, t = 0;
            float a =0, a1 =0, a2 = 0;
            if (dataGridViewX2.CurrentRow.Cells[7].Value.ToString() != "")
            {
                l = Convert.ToInt32(dataGridViewX2.CurrentRow.Cells[7].Value.ToString());
            }
            if (dataGridViewX2.CurrentRow.Cells[8].Value.ToString() != "")
            {
                med = Convert.ToInt32(dataGridViewX2.CurrentRow.Cells[8].Value.ToString());
            }
            if (dataGridViewX2.CurrentRow.Cells[9].Value.ToString() != "")
            {
                sm = Convert.ToInt32(dataGridViewX2.CurrentRow.Cells[9].Value.ToString());
            }
            if (dataGridViewX2.CurrentRow.Cells[9].Value.ToString() != "")
            {
                t = Convert.ToInt32(dataGridViewX2.CurrentRow.Cells[10].Value.ToString());
            }
            if (dataGridViewX2.CurrentRow.Cells[5].Value.ToString() != "")
            {
               a = float.Parse(dataGridViewX2.CurrentRow.Cells[5].Value.ToString());
            }
            if (dataGridViewX2.CurrentRow.Cells[11].Value.ToString() != "")
            {
                a1 = float.Parse(dataGridViewX2.CurrentRow.Cells[11].Value.ToString());
            }
            if (dataGridViewX2.CurrentRow.Cells[12].Value.ToString() != "")
            {
                a2 = float.Parse(dataGridViewX2.CurrentRow.Cells[12].Value.ToString());
            }
            float a3 = ((l * TrongLuongNet("Large")) + (med * TrongLuongNet("Medium")) + (sm * TrongLuongNet("Small")) + (t * TrongLuongNet("Tiny")));
            dataGridViewX2.CurrentRow.Cells[6].Value = a - a1 - a2 - a3;
            //a3=PackgageNet
            //NLoss,PLoss

            float nloss = ((l * TrongLuongNLoss("Large")) + (med * TrongLuongNLoss("Medium")) + (sm * TrongLuongNLoss("Small")) + (t * TrongLuongNLoss("Tiny")));
            dataGridViewX2.CurrentRow.Cells[11].Value = nloss;
            float ploss = ((l * TrongLuongPLoss("Large")) + (med * TrongLuongPLoss("Medium")) + (sm * TrongLuongPLoss("Small")) + (t * TrongLuongPLoss("Tiny")));
            dataGridViewX2.CurrentRow.Cells[12].Value = ploss;
        }

        //Cac ham bo tro
        float TrongLuongNet(string loaikhay)
        {
            float trongluong = 0;
            IList<Package> list = Platform.Model.Package.Objects.GetAll();
            foreach (Package a in list)
            {
                if (a.PckName == loaikhay)
                {
                    trongluong = a.Net;
                }
            }
            return trongluong;

        }
        float TrongLuongNLoss(string loaikhay)
        {
            float trongluong = 0;
            IList<Package> list = Platform.Model.Package.Objects.GetAll();
            foreach (Package a in list)
            {
                if (a.PckName == loaikhay)
                {
                    trongluong = a.NLoss;
                }
            }
            return trongluong;

        }
        float TrongLuongPLoss(string loaikhay)
        {
            float trongluong = 0;
            IList<Package> list = Platform.Model.Package.Objects.GetAll();
            foreach (Package a in list)
            {
                if (a.PckName == loaikhay)
                {
                    trongluong = a.PLoss;
                }
            }
            return trongluong;

        }

        //xu ly PICKNO
        private void btnPickNo_Click(object sender, EventArgs e)
        {
           // PickingDetail a = new PickingDetail();
            int cout = dataGridViewX2.Rows.Count;
            string supId = "";

            bool kqupdate = false;
            PickingList p = new PickingList();
            int pickingid = Convert.ToInt32(dataGridViewX2.Rows[0].Cells[1].Value.ToString());
            if (cout > 0)
            {
                p = PickingList.Objects.First(x => x.ID == pickingid);
            }
            p.Gross = 0;
            p.Net = 0;
            p.NLoss = 0;
            p.PLoss = 0;
            p.Package = 0;
            for (int i = 0; i < cout - 1; i++)
            {
                PickingDetail a = new PickingDetail();

                a.ID = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[16].Value.ToString());
                a.PickingTime = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[0].Value.ToString());
                a.PickingId = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[1].Value.ToString());
                a.SupId = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[2].Value.ToString());
                a.Gross = float.Parse(dataGridViewX2.Rows[i].Cells[5].Value.ToString());
                a.Net = float.Parse(dataGridViewX2.Rows[i].Cells[6].Value.ToString());
                a.Large = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[7].Value.ToString());
                a.Medium = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[8].Value.ToString());
                a.Small = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[9].Value.ToString());
                a.Tiny = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[10].Value.ToString());
                a.NLoss = float.Parse(dataGridViewX2.Rows[i].Cells[11].Value.ToString());
                a.PLoss = float.Parse(dataGridViewX2.Rows[i].Cells[12].Value.ToString());
                //string s = dataGridViewX2.Rows[i].Cells[14].Value.ToString();
                //if (s == "")
                //{
                //    a.WeightedBy = 0;
                //}
                //else
                //{
                //    a.WeightedBy = Convert.ToInt32(dataGridViewX2.Rows[i].Cells[14].Value.ToString());
                //}
                a.LastUpdated = DateTime.Now;
                //a.Notes = dataGridViewX2.Rows[i].Cells[15].Value.ToString();



                p.Gross += a.Gross;
                p.Net += a.Net;
                p.NLoss += a.NLoss;
                p.PLoss += a.PLoss;
                p.Package += (a.Large + a.Small + a.Medium + a.Tiny);

                PickingDetail pd = PickingDetail.Objects.FirstOrDefault(x => x.ID == a.ID);
                if (pd != null)
                {
                    // cap nhat pickDetail
                    bool rs = PickingDetail.Objects.Update(a);
                }
                else
                {
                    PickingDetail.Objects.Insert(a);
                }
                // Load, doa nay dung roi
                supId = a.SupId + "";


            }
            // cap nhat pickList
            kqupdate = PickingList.Objects.Update(p);
        }

        //Xử lí In ra file Excel
        private void btnPrint_Click(object sender, EventArgs e)
        {
            // tạo ứng dụng Excel
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            // Tạo WorkBook mới
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            // tạo Sheet nới
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            app.Visible = true;
            //luu file
            app = new Microsoft.Office.Interop.Excel.Application();

            //Khai báo Sheet đầu tiên để làm việc
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;
            // Thay đổi tên Sheet
            worksheet.Name = "Chi tiet lan can";
            //
            worksheet.Cells[1, 10] = "Thông Tin Chi Tiết Các Lần Cân Hàng ";
            worksheet.Cells[1, 10].Font.Bold = true;
            // Lưu trữ dữ liệu cho dòng header
            for (int i = 1; i < dataGridViewX2.Columns.Count + 1; i++)
                worksheet.Cells[2, i] = dataGridViewX2.Columns[i - 1].HeaderText;
            worksheet.Rows[2].Interior.ColorIndex = 15;
            //ke vien
            // Kẻ viền
            for (int i = 2; i < dataGridViewX2.Columns.Count + 1; i++)
                worksheet.Rows[i].Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
            // Lưu trữ dữ liệu cho các dòng, cột
            for (int i = 0; i < dataGridViewX2.Rows.Count - 1; i++)
                for (int j = 0; j < dataGridViewX2.Columns.Count; j++)
                    worksheet.Cells[i + 4, j + 1] = dataGridViewX2.Rows[i].Cells[j].Value.ToString();
            // Lưu file
            //workbook.SaveAs("D:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // Thoát khỏi ứng dụng Excel
            app.Quit();

        }

        //gán giá trị cho pickinglist
        private void dataGridViewX1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //List<PickingDetail> l1 = PickingDetail.Objects.GetAll();
            //List<PickingDetail> l2 = new List<PickingDetail>();
            //List<PickingList> l3 = PickingList.Objects.GetAll();
            //List<PickingList> l4 = new List<PickingList>();
            //foreach (PickingDetail b in l2)
            //{
            //    foreach (PickingList a in l4)
            //    {
            //        float gros = b.Gross;
            //        dataGridViewX1.CurrentRow.Cells[7].Value = gros;
            //        //float net = b.Net;
            //        //dataGridViewX1.CurrentRow.Cells[8].Value = net;
            //        //float packge = b.Large + b.Medium + b.Small + b.Tiny;
            //        //dataGridViewX1.CurrentRow.Cells[9].Value = packge;
            //        //float plos = b.PLoss;
            //        //dataGridViewX1.CurrentRow.Cells[11].Value = plos;
            //        //float nlos = b.NLoss;
            //        //dataGridViewX1.CurrentRow.Cells[10].Value = nlos;
            //        //PickingList.Objects.Update(a);
            //    }
            //}
            //PickingDetail a = new PickingDetail();
            //PickingList p = new PickingList();
            //p = PickingList.Objects.First(x => x.ID == a.PickingId);
            //p.Gross = p.Gross + a.Gross;
            //p.Net = p.Net + a.Net;
            //p.NLoss = p.NLoss + a.NLoss;
            //p.PLoss = p.PLoss + a.PLoss;
            //p.Package = p.Package + a.Large + a.Small + a.Medium + a.Tiny;
            //PickingDetail.Objects.Update(a);
            //PickingList.Objects.Update(p);
        }

        //load combobox
        void LoadCCB()
        {
            cbKieuTimKiem.Items.Insert(0, "--Chọn kiểu tìm kiếm--");
            cbKieuTimKiem.SelectedIndex = 0;
        }
        private void cbKieuTimKiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = cbKieuTimKiem.SelectedIndex;
            switch (i)
            {
                case 0:
                    txtTimKiemMH.Visible = false;
                    btnTimKiemMH.Visible = false;
                    txtTimKiemNCC.Visible = false;
                    btnTimKiemNCC.Visible = false;
                    break;
                case 1:
                    txtTimKiemMH.Visible = false;
                    btnTimKiemMH.Visible = false;
                    txtTimKiemNCC.Visible = true;
                    btnTimKiemNCC.Visible = true;
                    break;
                case 2:
                    txtTimKiemMH.Visible = true;
                    btnTimKiemMH.Visible = true;
                    txtTimKiemNCC.Visible = false;
                    btnTimKiemNCC.Visible = false;
                    break;
            }
        }
        //xử lí button tìm kiếm mặt hàng
        private void btnTimKiemMH_Click(object sender, EventArgs e)
        {
            if (txtTimKiemMH.Text != "")
            {
                List<Article> list = Article.Objects.Where(c => c.ArtName.Contains(txtTimKiemMH.Text)).ToList();
                dgArticle.DataSource = list;
            }
            else
            {
                MessageBox.Show("Bạn chưa nhập dữ liệu tìm kiếm");
            }
        }

        //xử lí button tìm kiếm nhà cung cấp
        private void btnTimKiemNCC_Click(object sender, EventArgs e)
        {
            if (txtTimKiemNCC.Text != "")
            {
                List<Supplier> list = Supplier.Objects.Where(c => c.LastName.Contains(txtTimKiemNCC.Text)).ToList();
                dgSupplier.DataSource = list;
                //dgSupplier.DataBindings();
            }
            else
            {
                MessageBox.Show("Bạn chưa nhập dữ liệu tìm kiếm");
            }
        }

        //xử lí button load lại form
        private void btnLoadForm_Click(object sender, EventArgs e)
        {
            List<Supplier> list = Supplier.Objects.GetAll();
            List<Article> lista = Article.Objects.GetAll();
            List<PickingList> listl = PickingList.Objects.GetAll();
            List<PickingDetail> listd = PickingDetail.Objects.GetAll();
            dgSupplier.DataSource = list;
            dgArticle.DataSource = lista;
            dataGridViewX1.DataSource = listl;
            dataGridViewX2.DataSource = listd;
            txtTimKiemMH.Visible = false;
            btnTimKiemMH.Visible = false;
            txtTimKiemNCC.Visible = false;
            btnTimKiemNCC.Visible = false;
        }
    }
}